//
//  AdSenseExampleAppDelegate.m
//  Google Ads iPhone publisher SDK.
//
//  Copyright Google Inc 2009. All rights reserved.
//

#import "AdSenseExampleAppDelegate.h"

// When SHOW_300x250_AD is set, we'll show the 300x250 ad format.  Otherwise, we
// will show the 320x50 format.
#define SHOW_300x250_AD 0

@implementation AdSenseExampleAppDelegate

@synthesize window = window_;
@synthesize mainViewController = mainViewController_;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  // Add our view controller
  UIView *mainView = [mainViewController_ view];
  [window_ addSubview:mainView];

  adViewController_ = [[GADAdViewController alloc] initWithDelegate:self];

#if SHOW_300x250_AD
  adViewController_.adSize = kGADAdSize300x250;
#else
  adViewController_.adSize = kGADAdSize320x50;
#endif

  // **************************************************************************
  // Please replace the kGADAdSenseClientID, kGADAdSenseKeywords, and
  // kGADAdSenseChannelIDs values with your own AdSense client ID, keywords,
  // and channel IDs, respectively. If this application has an associated
  // iPhone website, then set the site's URL using kGADAdSenseAppWebContentURL
  // for improved ad targeting.
  //
  // Please also replace the kGADAdSenseCompanyName, kGADAdSenseAppName and
  // kGADAdSenseApplicationAppleID with your own company name, application name,
  // and Application Apple ID respectively.
  //
  // PLEASE DO NOT CLICK ON THE AD UNLESS YOU ARE IN TEST MODE. OTHERWISE, YOUR
  // ACCOUNT MAY BE DISABLED.
  // **************************************************************************
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
      @"ca-mb-app-pub-nnnnnnnnnnnnnnnn", kGADAdSenseClientID,
      @"Company Name", kGADAdSenseCompanyName,
      @"App Name", kGADAdSenseAppName,
      @"0", kGADAdSenseApplicationAppleID,
      @"hotels+san+francisco", kGADAdSenseKeywords,
      [NSArray arrayWithObjects:@"06562723305", nil], kGADAdSenseChannelIDs,
      [NSNumber numberWithInt:1], kGADAdSenseIsTestAdRequest,
      nil];

  // Loading the ad will create the ad view.
  [adViewController_ loadGoogleAd:attributes];

  // Position ad at bottom of screen.
  UIView *adView = adViewController_.view;
  CGFloat adViewHeight = [adView bounds].size.height;
  CGRect bounds = [mainView bounds];
  CGPoint center = CGPointMake(CGRectGetMidX(bounds),
                               CGRectGetMaxY(bounds) - adViewHeight / 2);
  [adView setCenter:center];
  [mainView addSubview:adView];
  [window_ makeKeyAndVisible];
}

- (void)dealloc {
  [adViewController_ release];
  [window_ release];
  [super dealloc];
}

- (UIViewController *)viewControllerForModalPresentation:
    (GADAdViewController *)adController {
  return mainViewController_;
}

// Set the ad click action to display the advertiser's site in a full-screen
// UIWebView.
- (GADAdClickAction)adControllerActionModelForAdClick:
    (GADAdViewController *)adController {
  return GAD_ACTION_DISPLAY_INTERNAL_WEBSITE_VIEW;
}

- (void)loadSucceeded:(GADAdViewController *)adController
          withResults:(NSDictionary *)results {
  // Successful load. You can examine the results for interesting things.
  NSLog(@"Load Succeeded: %@", results);
}

- (void)loadFailed:(GADAdViewController *)adController
         withError:(NSError *) error {
	// Handle error here.
}

@end

